﻿namespace Behemoth.SharePoint.Logging {

  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;

  using Microsoft.SharePoint.Administration;

  /// <summary>
  /// Put this code in your class that produces log entries, or inherit 
  /// this class and create your own LOGGING_PRODUCT and LOGGING_CATEGORY.
  /// </summary>
  public abstract class LoggingEventConsumerBase {

    #region Logging Code Pattern v2.0

    /// <summary>
    /// 
    /// </summary>
    /// <param name="e"></param>
    public event LoggingEventHandler Logging;

    protected abstract string LOGGING_PRODUCT {
      get; // "Behemoth";
    }
    protected abstract string LOGGING_CATEGORY {
      get; // "Logging Example";
    }

    /// <summary>
    /// Put a method like this in your class that produces log entries
    /// </summary>
    /// <param name="e"></param>
    protected virtual void Log(LoggingEventArgs e) {
      // if nobody could be bothered to attach their own events, we should
      // still log things in the default way
      if (Logging == null)
        Logging += new LoggingEventHandler(BehemothLoggingService.Default.Log);
      if (Logging != null)
        Logging(this, e);
    }
    protected virtual void Log(string msg) {
      Log(msg, TraceSeverity.Verbose);
    }
    protected virtual void Log(string msg, TraceSeverity severity) {
      LoggingEventArgs e = new LoggingEventArgs(LOGGING_CATEGORY, LOGGING_PRODUCT, msg, severity);
      Log(e);
    }
    protected virtual void Log(Exception ex) {
      LoggingEventArgs e = new LoggingEventArgs(LOGGING_CATEGORY, LOGGING_PRODUCT, ex);
      Log(e);
    }

    #endregion Logging Code Pattern

  } // class

} // namespace
